<?php
require_once dirname(__FILE__) . '/../../lib/XS.class.php';

/**
 * Sample class to extend XSComponent for testing
 */
class XSSample extends XSComponent
{
	private $_normal = 'normal';
	private $_tradition = '_tradition';
	public $tradition = 'tradition';

	public function getReadonly()
	{
		return true;
	}

	public function setWriteonly($value)
	{
		
	}

	public function getNormal()
	{
		return $this->_normal;
	}

	public function setNormal($value)
	{
		$this->_normal = $value;
	}

	public function getTradition()
	{
		return $this->_tradition;
	}

	public function setTradition($value)
	{
		$this->_tradition = $value;
	}
}

/**
 * Test class for XSComponent.
 * Generated by PHPUnit on 2011-07-23 at 20:26:04.
 */
class XSComponentTest extends PHPUnit_Framework_TestCase
{
	/**
	 * @var XSComponent
	 */
	protected $object;

	/**
	 * Sets up the fixture, for example, opens a network connection.
	 * This method is called before a test is executed.
	 */
	protected function setUp(): void
	{
		$this->object = new XSSample;
	}

	/**
	 * Tears down the fixture, for example, closes a network connection.
	 * This method is called after a test is executed.
	 */
	protected function tearDown(): void
	{
		
	}

	public function test__get()
	{
		// normal
		$this->assertTrue($this->object->readOnly);
		$this->assertTrue($this->object->readonly);
		$this->assertEquals('normal', $this->object->normal);
		$this->assertEquals('tradition', $this->object->tradition);
		$this->assertEquals('_tradition', $this->object->getTradition());

		// undefined
		$this->expectException(XSException::class);
		$this->expectExceptionMessage('Undefined property: XSSample::$test');
		$this->expectExceptionCode(0);
		$test = $this->object->test;
	}

	public function test__get2()
	{
		// write-only
		$this->expectException(XSException::class);
		$this->expectExceptionMessage('Write-only property: XSSample::$writeOnly');
		$this->expectExceptionCode(0);
		$test = $this->object->writeOnly;
	}

	public function test__set()
	{
		// normal
		$this->assertEquals('normal', $this->object->normal);
		$this->object->normal = 'disnormal';
		$this->assertEquals('disnormal', $this->object->normal);

		// undefiend
		$this->expectException(XSException::class);
		$this->expectExceptionMessage('Undefined property: XSSample::$test');
		$this->expectExceptionCode(0);
		$this->object->test = 'test';
	}

	public function test__set2()
	{
		// readonly
		$this->expectException(XSException::class);
		$this->expectExceptionMessage('Read-only property: XSSample::$readOnly');
		$this->expectExceptionCode(0);
		$this->object->readOnly = false;
	}

	/**
	 * Check getter only
	 */
	public function test__isset()
	{
		$this->assertTrue(isset($this->object->readOnly));
		$this->assertFalse(isset($this->object->writeOnly));
		$this->assertTrue(isset($this->object->normal));
		$this->assertFalse(isset($this->object->test));
	}

	/**
	 * try for normal
	 */
	public function test__unset()
	{
		unset($this->object->normal);
		$this->assertNull($this->object->normal);
		$this->assertTrue(isset($this->object->normal));

		$this->assertEquals('tradition', $this->object->tradition);
		unset($this->object->tradition);

		$this->assertNotNull($this->object->tradition);
		$this->assertTrue(isset($this->object->tradition));
		$this->assertEquals('_tradition', $this->object->tradition);

		unset($this->object->tradition);
		$this->assertNull($this->object->tradition);
	}
}
